<template>
  <a-drawer :closable="false" :title="title" :visible="visible" :width="width" destroyOnClose placement="right" @close="close">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="主键">
          {{ model.id }}
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标识">
          <a-input v-decorator="['code', validatorRules.code]" placeholder="请输入标识"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
          <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入名称"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="服务类型">
          <a-select
            v-decorator="['type', validatorRules.type]"
            placeholder="请选择服务类型"
            @change="val => typeChange(val)">
            <a-select-option v-for="(type, index) in serviceTypeOptions" :key="type.value">{{ type.text }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="图标">
          <j-image-upload v-decorator="['icon', validatorRules.topology]" :trigger-change="true"></j-image-upload>
        </a-form-item>
        <a-form-item v-show="show.host" :labelCol="labelCol" :wrapperCol="wrapperCol" label="HOST">
          <a-input v-decorator="['host', validatorRules.host]" placeholder="请输入HOST"></a-input>
        </a-form-item>
        <a-form-item v-show="show.port" :labelCol="labelCol" :wrapperCol="wrapperCol" label="端口号">
          <a-input-number v-decorator="['port', validatorRules.port]" placeholder="请输入端口号" style="width: 100%" />
        </a-form-item>
        <a-form-item v-show="show.threadNum" :labelCol="labelCol" :wrapperCol="wrapperCol" label="线程数">
          <a-input-number v-decorator="['threadNum', validatorRules.threadNum]" placeholder="请输入线程数" style="width: 100%" />
        </a-form-item>
        <a-form-item v-show="show.isSync" :labelCol="labelCol" :wrapperCol="wrapperCol" label="同步处理">
          <j-dict-select-tag v-decorator="['isSync', validatorRules.isSync]" :trigger-change="true" dictCode="activiti_sync" placeholder="是否同步处理" type="radio" />
        </a-form-item>
        <a-form-item v-show="show.resoleMethod" :labelCol="labelCol" :wrapperCol="wrapperCol" label="解析方式">
          <a-input v-decorator="['resolMethod', validatorRules.resolMethod]" placeholder="请输入解析方式"></a-input>
        </a-form-item>
        <a-form-item v-show="show.username" :labelCol="labelCol" :wrapperCol="wrapperCol" label="用户名">
          <a-input v-decorator="['username', validatorRules.username]" placeholder="请输入用户名"></a-input>
        </a-form-item>
        <a-form-item v-show="show.password" :labelCol="labelCol" :wrapperCol="wrapperCol" label="密码">
          <a-input v-decorator="['password', validatorRules.password]" placeholder="请输入密码"></a-input>
        </a-form-item>
        <a-form-item v-show="show.topics" :labelCol="labelCol" :wrapperCol="wrapperCol" label="订阅主题">
          <a-input v-decorator="['topics', validatorRules.topics]" placeholder="请输入订阅主题"></a-input>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="描述">
          <a-textarea v-decorator="['description', validatorRules.description]" placeholder="请输入描述" rows="4" />
        </a-form-item>
      </a-form>
    </a-spin>

    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1
      }"
    >
      <a-button :style="{ marginRight: '10px' }" type="primary" @click="handleOk">确定</a-button>
      <a-button type="" @click="handleCancel">取消</a-button>
    </div>
  </a-drawer>
</template>

<script>
import {httpAction} from '@/api/manage'
import pick from 'lodash.pick'
import {validateDuplicateValue} from '@/utils/util'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JImageUpload from '../../../../components/jeecg/JImageUpload'

export default {
  name: 'NetworkServiceModal',
  components: {
    JImageUpload,
    JDictSelectTag
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: '操作',
      width: 500,
      visible: false,
      serviceTypeOptions: [],
      model: {
        host: window._CONFIG['domianURL'],
        port: '8888',
        icon: '',
        isSync: 0,
        threadNum: 1
      },
      show: {
        host: false,
        port: false,
        threadNum: false,
        isSync: false,
        resoleMethod: false,
        topics: false,
        password: false,
        username: false,
        clientId: false
      },

      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        code: {
          rules: [{ required: true, message: '请输入标识!' }, { validator: (rule, value, callback) => validateDuplicateValue('iot_network_service', 'code', value, this.model.id, callback) }]
        },
        name: {
          rules: [{ required: true, message: '请输入名称!' }]
        },
        type: {
          rules: [{ required: true, message: '请输入服务类型!' }]
        },
        icon: {
          rules: []
        },
        host: {
          rules: [{required: true, message: '请输入端IP地址!'},
            {pattern: /^(2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2}$/, message: '请输入端正确IP地址!'}]
        },
        port: {
          rules: [
            { required: true, message: '请输入端口号!' },
            { pattern: /^-?\d+$/, message: '请输入整数!' },
            { validator: (rule, value, callback) => validateDuplicateValue('iot_network_service', 'port', value, this.model.id, callback) }
          ]
        },
        threadNum: {
          rules: [
            { required: false, message: '请输入线程数!' },
            { pattern: /^-?\d+$/, message: '请输入整数!' }
          ]
        },
        isSync: {
          rules: [{ required: false, message: '请输入是否同步处理!' }]
        },
        resoleMethod: {
          rules: []
        },
        username: {
          rules: []
        },
        password: {
          rules: []
        },
        topics: {
          rules: []
        },
        description: {
          rules: []
        }
      },
      url: {
        add: '/network/manage/add',
        edit: '/network/manage/edit',
        getServiceType: '/network/manage/getServiceType'
      }
    }
  },
  computed: {
    serviceType() {
      return this.model.type
    }
  },
  watch: {
    serviceType(val, oval) {
      this.typeChange(val)
    }
  },
  mounted() {
  },
  methods: {
    typeChange(val) {
      switch (val) {
        case 'tcp-server':
          this.show.host = true
          this.show.port = true
          this.show.resolMethod = false
          this.show.threadNum = false
          this.show.isSync = false
          this.show.topics = false
          this.show.password = false
          this.show.username = false
          break
        case 'mqtt-server':
          this.show.host = true
          this.show.port = true
          this.show.threadNum = false
          this.show.isSync = false
          this.show.resoleMethod = false
          this.show.topics = false
          this.show.password = false
          this.show.username = false
          break
        case 'tcp-client':
          this.show.host = true
          this.show.port = true
          this.show.threadNum = false
          this.show.isSync = false
          this.show.resoleMethod = false
          this.show.topics = false
          this.show.password = false
          this.show.username = false
          break
        case 'mqtt-client':
          this.show.host = true
          this.show.port = true
          this.show.threadNum = false
          this.show.isSync = false
          this.show.resoleMethod = false
          this.show.topics = true
          this.show.password = true
          this.show.username = true
          break
        case 'default':
          this.show.host = false
          this.show.port = false
          this.show.threadNum = false
          this.show.isSync = false
          this.show.resoleMethod = false
          this.show.topics = false
          this.show.password = false
          this.show.username = false
          break
      }
    },
    add() {
      this.edit(this.model)
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.serviceTypeOptions = this.$parent.serviceTypeOptions
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(this.model, 'code', 'name', 'type', 'icon', 'host', 'port', 'threadNum', 'isSync', 'resolMethod', 'clientId', 'username', 'password', 'topics', 'createTime', 'description')
        )
      })
    },
    close() {
      this.$emit('close')
      this.edit({})
      this.show.host = false
      this.show.port = false
      this.show.threadNum = false
      this.show.isSync = false
      this.show.resoleMethod = false
      this.show.topics = false
      this.show.password = false
      this.show.username = false
      this.show.clientId = false
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          /* 隐藏的内容置空 */
          for (let key in formData) {
            if (that.show[key] && !that.show[key]) {
              console.log(key)
              formData[key] = ''
            }
          }
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'code', 'name', 'type', 'icon', 'host', 'port', 'threadNum', 'isSync', 'resolMethod', 'username', 'password', 'topics', 'createTime', 'description'))
    }
  }
}
</script>

<style lang="less" scoped></style>